<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Controller;
use App\Models\Shop\Category;
use Illuminate\Http\Request;

use App\Http\Requests\StoreCategory;
use App\Http\Requests\UpdateCategory;
use Illuminate\Support\Facades\Cache;

class CategoryController extends Controller
{
    public function __construct()
    {
        view()->share([
            'categories' => Category::get_categories(),

            '_shop'=>'am-in',
            '_category'=>'am-active',
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $categoies = Category::where('parent_id',0)->orderBy('sort_order')->get();
//        foreach ($categoies as $k=>$v){
//            $categoies[$k]['children'] = Category::where('parent_id',$v['id'])->orderBy('sort_order')->get();
//        }
        return view('admin.shop.category.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.category.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreCategory $request)
    {
        Category::create($request->all());
        Cache::clear();
        return redirect('/admin/shop/category')->with('success', '新增品牌分类成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cate = Category::find($id);
        Cache::clear();
        return view('admin.shop.category.edit', compact('cate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateCategory $request, $id)
    {
        $category = Category::find($id);
        $category->update($request->all());
        Cache::clear();
        return redirect(route('shop.category.index'))->with('success', '编辑分类成功！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //模型删除
//        if(!Category::check_children($id)) {
//            return back()->with('error', '此分类下有二级分类，不能删除');
//        }
//        Category::destroy($id);
//        return back()->with('success', '删除分类成功');

        //一般删除
//        $category = Category::with('children')->find($id);
//        if($category->children->isEmpty()){
//            Category::destroy($id);
//            return back()->with('success', '删除分类成功');
//        }
//        return back()->with('error', '此分类下有二级分类，不能删除');

        //ajax删除
        $category = Category::with('children')->find($id);
        if($category->children->isEmpty()){
            Category::destroy($id);
            Cache::clear();
            $info = array('status'=>1,'msg'=>'删除分类成功');
        }else{
            $info = array('status'=>0,'msg'=>'此分类下有二级分类，不能删除');
        }
        return $info;
    }

    /**
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $category = Category::find($request->id);
        Cache::clear();
        $attr = $request->attr;
        $category->$attr = !$category->$attr;
        $category->save();
    }

    /**
     * 排序
     */
    public function sort_order(Request $request){
        $category = Category::find($request->id);
        Cache::clear();
        $category->sort_order = $request->sort_order;
        $category->save();
    }
}
